Custom hooks are used to encapsulate and reuse stateful logic across multiple components. To create a custom hook, follow these steps:
- Start with 'use': prefix the Name of the custom hook with 'use' to make it clear that it's a hook.
- Reuse Existing Hooks: Custom hooks can internally use built-in hooks to achieve functionality.
Custom hooks are used for Sharing stateful logic and effects between components, They deal with some external system or a browser API that is not built into react like:
- Change the document title based on rendered values
- Get or set values from local storage
- Run analytics events
- Show some greetings based on the time of day
- Focus on a form field after the load